package com.deep.spring.stream.io1;


import org.dom4j.*;
import org.dom4j.io.*;
import org.springframework.core.io.UrlResource;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class UrlResourceTest {
    public static void main(String[] args)
            throws Exception {
        v2();
//        v1();
    }

    public static void v1() throws IOException, DocumentException {
        // 创建一个Resource对象，指定从文件系统里读取资源
        UrlResource ur = new UrlResource("file:E:/book.xml");
        // 获取该资源的简单信息
        System.out.println(ur.getFilename());
        System.out.println(ur.getDescription());
        // 创建基于SAX的dom4j解析器
        SAXReader reader = new SAXReader();
        Document doc = reader.read(ur.getFile());
        // 获取根元素
        Element el = doc.getRootElement();
        List l = el.elements();
        // 遍历根元素的全部子元素
        for (Iterator it = l.iterator(); it.hasNext(); ) {
            // 每个节点都是<书>节点
            Element book = (Element) it.next();
            List ll = book.elements();
            // 遍历<书>节点的全部子节点
            for (Iterator it2 = ll.iterator(); it2.hasNext(); ) {
                Element eee = (Element) it2.next();
                System.out.println(eee.getText());
            }
        }
    }

    public static void v2() throws IOException, DocumentException {
        UrlResource urlResource = new UrlResource("http://localhost/book.xml");
        System.out.println("urlResource.getFilename()=" + urlResource.getFilename());
        System.out.println("urlResource.getDescription()=" + urlResource.getDescription());
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(urlResource.getInputStream());
        // 获取根元素
        Element el = document.getRootElement();
        List l = el.elements();
        // 遍历根元素的全部子元素
        for (Iterator it = l.iterator(); it.hasNext(); ) {
            // 每个节点都是<书>节点
            Element book = (Element) it.next();
            List ll = book.elements();
            // 遍历<书>节点的全部子节点
            for (Iterator it2 = ll.iterator(); it2.hasNext(); ) {
                Element eee = (Element) it2.next();
                System.out.println(eee.getText());
            }
        }
    }
}